package com.tz.service.impl;


import com.tz.entity.User;
import com.tz.mapper.PersonalCenterMapper;
import com.tz.service.PerSonalCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * 本类用来实现个人中心
 *
 * @author Cunese
 * @date 17-12-29
 */
@Service
@Transactional
public class PersonalCenterServiceImpl implements PerSonalCenterService {
    @Autowired
    PersonalCenterMapper personalCenterMapper;

    /**
     *修改用户信息
     */
    @Override
    public void updateUser(User user) {
        personalCenterMapper.updateUser(user);
    }

    /**
     *上传用户头像
     */
    @Override
    public void upload(String path, MultipartFile file) throws IOException {
        File file1 = new File(path + File.separator + file.getOriginalFilename());
        if (!file1.exists()) {
            file1.createNewFile();
            InputStream inputStream = file.getInputStream();
            FileOutputStream fileOutputStream = new FileOutputStream(file1);
            byte[] bytes = new byte[1024];
            while (inputStream.read(bytes) != -1) {
                fileOutputStream.write(bytes);
            }
            inputStream.close();
            fileOutputStream.close();
        }
    }

}
